package com.ssyx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssyx.acl.mapper.PermissionMapper;
import com.ssyx.acl.service.PermissionService;
import com.ssyx.acl.service.RolePermissionService;
import com.ssyx.acl.utils.PermissionHelper;
import com.ssyx.model.acl.Permission;
import com.ssyx.model.acl.RolePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:PermissionService
 * Description:
 *
 * @Author kay
 * @Create 2024/4/24 23:22
 * @Version 1.0
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
        implements PermissionService {

    @Autowired
    private RolePermissionService rolePermissionService;

    //获取菜单(树型)
    @Override
    public List<Permission> getAllMenu() {
        //查找所有菜单,根据id进行排序
//        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
//        wrapper.orderByAsc(Permission::getId);
        //将所有菜单封装到AllPermissionList中
        List<Permission> AllPermissionList = baseMapper.selectList(null);

        //构成树型结构
        List<Permission> tree = PermissionHelper.build(AllPermissionList);
        return tree;
    }


    //递归删除菜单
    @Override
    public void removeChildById(Long id) {
        //创建List集合idList,封装所有要删除菜单的id
        List<Long> idList = new ArrayList<>();

        //重点:递归查找当前菜单的子菜单
        getAllPermissionId(id,idList);

        //当前菜单也要删除
        idList.add(id);
        //调用方法,根据多个菜单id删除
        baseMapper.deleteBatchIds(idList);
    }

    //递归查找当前菜单的子菜单
    //id:当前菜单id  idList:最终封装的List集合,包含所有菜单id
    private void getAllPermissionId(Long id, List<Long> idList) {
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Permission::getPid,id);
        List<Permission> childList = baseMapper.selectList(wrapper);
        //递归查询是否还有子菜单
        childList.stream().forEach(item ->{
            idList.add(item.getId());
            //递归
            getAllPermissionId(item.getId(),idList);
        });
    }

    //根据角色id获取菜单信息
    @Override
    public List<Permission> getPermissionByRoleId(Long roleId) {
        //allPermissions
        //将所有菜单封装到AllPermissionList中
        List<Permission> AllPermissionList = baseMapper.selectList(null);

        //
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(RolePermission::getPermissionId).eq(RolePermission::getRoleId,roleId);
        List<Long> permissionIdList = rolePermissionService.listObjs(wrapper);
        for(Permission permission : AllPermissionList){
            for(Long permissionId : permissionIdList){
                if(permission.getId() == permissionId){
                    permission.setSelect(true);
                }
            }
        }

        List<Permission> tree = PermissionHelper.build(AllPermissionList);
        return tree;

    }

    //给指定角色分配菜单权限
    @Override
    public void saveRolePermission(Long roleId, Long[] permissionIds) {
        //先删除已分配过的菜单权限
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId,roleId);
        rolePermissionService.remove(wrapper);

        //分配新的菜单
        List<RolePermission> RolePermissionList = new ArrayList<>();
        for(Long permissionId : permissionIds){
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            RolePermissionList.add(rolePermission);
        }
        rolePermissionService.saveBatch(RolePermissionList);
    }
}
